﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour {
	public Monster target;
	public float speed=10;
 	public Tower self;
	public float rotSpeed = 20;
	public void SetTarget(Monster target,Tower tower){
		this.target=target;
		self=tower;
	}
	 private void Update() {
		if(!target){
			 Release();
			return;
		}

		Quaternion q = Quaternion.LookRotation((target.transform.position+Vector3.up*1.5f - transform.position).normalized);
		transform.rotation =Quaternion.Lerp(transform.rotation,q,Time.deltaTime*rotSpeed) ;
		transform.Translate(transform.forward*(speed+10)*Time.deltaTime,Space.World);
	}

    private void OnTriggerEnter(Collider other) {
	
		if(target==null){
			Release();
			return;
		}
		if(other.CompareTag("Enemy")){
			var damage= other.GetComponent<IDamage>();
			if(damage!=null){
				damage.ToDamage(self.attack,self,transform.position);
			}
			Release();
			
		}
		else if(other.tag=="Ground"){
			Release();
		}
		//发射特效

	}

	public void Release(){
		gameObject.SetActive(false);
		target=null;
		self=null;
	}

}
